use*_*261 40 java mysql sql jdbc connectivity
我有这个Java程序: MySQLConnectExample.java
import java.sql.*;
import java.util.Properties;
public class MySQLConnectExample {
public static void main(String[] args) {
Connection conn1 = null;
Connection conn2 = null;
Connection conn3 = null;
try {
String url1 = "jdbc:mysql://localhost:3306/aavikme";
String user = "root";
String password = "aa";
conn1 = DriverManager.getConnection(url1, user, password);
if (conn1 != null)
System.out.println("Connected to the database test1");
String url2 = "jdbc:mysql://localhost:3306/aavikme?user=root&password=aa";
conn2 = DriverManager.getConnection(url2);
if (conn2 != null) {
System.out.println("Connected to the database test2");
}
String url3 = "jdbc:mysql://localhost:3306/aavikme";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "aa");
conn3 = DriverManager.getConnection(url3, info);
if (conn3 != null) {
System.out.println("Connected to the database test3");
}
} catch (SQLException ex) {
System.out.println("An error occurred. Maybe user/password is invalid");
ex.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我这样编译:
E:\java mysql code driver>javac MySQLConnectExample.java
E:\java mysql code driver>java -cp mysql-connector-java-3.0.11-stable-bin.jar;.
MySQLConnectExample
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
An error occurred. Maybe user/password is invalid
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/
aavikme
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at MySQLConnectExample.main(MySQLConnectExample.java:20)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
Ada*_*kin 68
确保先运行此命令:
Class.forName("com.mysql.jdbc.Driver");
Run Code Online (Sandbox Code Playgroud)
这会强制驱动程序自行注册,以便Java知道如何处理这些数据库连接字符串.
有关更多信息,请参阅MySQL Connector参考.
unk*_*own 10
你必须加载jdbc driver.请考虑以下代码.
try {
Class.forName("com.mysql.jdbc.Driver");
// connect way #1
String url1 = "jdbc:mysql://localhost:3306/aavikme";
String user = "root";
String password = "aa";
conn1 = DriverManager.getConnection(url1, user, password);
if (conn1 != null) {
System.out.println("Connected to the database test1");
}
// connect way #2
String url2 = "jdbc:mysql://localhost:3306/aavikme?user=root&password=aa";
conn2 = DriverManager.getConnection(url2);
if (conn2 != null) {
System.out.println("Connected to the database test2");
}
// connect way #3
String url3 = "jdbc:mysql://localhost:3306/aavikme";
Properties info = new Properties();
info.put("user", "root");
info.put("password", "aa");
conn3 = DriverManager.getConnection(url3, info);
if (conn3 != null) {
System.out.println("Connected to the database test3");
}
} catch (SQLException ex) {
System.out.println("An error occurred. Maybe user/password is invalid");
ex.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
从具有列column1,column2,column3 column4,cloumn1和2的表中检索数据的示例保存int值,第3列和第4列保持 varchar(10)
import java.sql.*;
// need to import this as the STEP 1. Has the classes that you mentioned
public class JDBCexample {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://LocalHost:3306/databaseNameHere";
// DON'T PUT ANY SPACES IN BETWEEN and give the name of the database (case insensitive)
// database credentials
static final String USER = "root";
// usually when you install MySQL, it logs in as root
static final String PASS = "";
// and the default password is blank
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// registering the driver__STEP 2
Class.forName("com.mysql.jdbc.Driver");
// returns a Class object of com.mysql.jdbc.Driver
// (forName(""); initializes the class passed to it as String) i.e initializing the
// "suitable" driver
System.out.println("connecting to the database");
// opening a connection__STEP 3
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// executing a query__STEP 4
System.out.println("creating a statement..");
stmt = conn.createStatement();
// creating an object to create statements in SQL
String sql;
sql = "SELECT column1, cloumn2, column3, column4 from jdbcTest;";
// this is what you would have typed in CLI for MySQL
ResultSet rs = stmt.executeQuery(sql);
// executing the query__STEP 5 (and retrieving the results in an object of ResultSet)
// extracting data from result set
while(rs.next()){
// retrieve by column name
int value1 = rs.getInt("column1");
int value2 = rs.getInt("column2");
String value3 = rs.getString("column3");
String value4 = rs.getString("columnm4");
// displaying values:
System.out.println("column1 "+ value1);
System.out.println("column2 "+ value2);
System.out.println("column3 "+ value3);
System.out.println("column4 "+ value4);
}
// cleaning up__STEP 6
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
// handle sql exception
e.printStackTrace();
}catch (Exception e) {
// TODO: handle exception for class.forName
e.printStackTrace();
}finally{
//closing the resources..STEP 7
try {
if (stmt != null)
stmt.close();
} catch (SQLException e2) {
e2.printStackTrace();
}try {
if (conn != null) {
conn.close();
}
} catch (SQLException e2) {
e2.printStackTrace();
}
}
System.out.println("good bye");
}
}
Run Code Online (Sandbox Code Playgroud)
我有同样的问题,我的代码如下:
private Connection conn = DriverManager.getConnection(Constant.MYSQL_URL, Constant.MYSQL_USER, Constant.MYSQL_PASSWORD);
private Statement stmt = conn.createStatement();
Run Code Online (Sandbox Code Playgroud)
我还没有加载驱动程序类,但是它可以在本地运行,我可以从MySQL查询结果,但是,当我将其部署到Tomcat时它不起作用,并且发生以下错误:
No suitable driver found for jdbc:mysql://172.16.41.54:3306/eduCloud
Run Code Online (Sandbox Code Playgroud)
因此,当我看到发布的其他答案时,如下所示加载了驱动程序类:
Class.forName("com.mysql.jdbc.Driver");
Run Code Online (Sandbox Code Playgroud)
现在可以使用!我不知道为什么它在本地效果很好,我需要您的帮助,非常感谢!
| 归档时间: |
|
| 查看次数: |
260828 次 |
| 最近记录: |