Java MySQL与JDBC的连接
我正在尝试连接到我计算机上的数据库。数据库的密码是root,用户也是。我的项目库中有连接器jar文件,我有7.0 jre和jdk,数据库“Testing1”中的表“clients”存在,有1个条目和3个字段Java MySQL与JDBC的连接,java,mysql,jdbc,Java,Mysql,Jdbc,我正在尝试连接到我计算机上的数据库。数据库的密码是root,用户也是。我的项目库中有连接器jar文件,我有7.0 jre和jdk,数据库“Testing1”中的表“clients”存在,有1个条目和3个字段 +--------------------+ | Tables_in_Testing1 | +--------------------+ | clients | | esk | | files | +-------
+--------------------+
| Tables_in_Testing1 |
+--------------------+
| clients |
| esk |
| files |
+--------------------+
客户表:
+----+------------------+-----------------------+
| id | PublicKey | Email |
+----+------------------+-----------------------+
| 1 | PublicKeyNumber1 | FirstClient@email.com |
+----+------------------+-----------------------+
下面是代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
class JDBCTest {
private static Connection con=null;
private static Statement st=null;
private static ResultSet rs=null;
public static void main(String args[])throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Testing1?user=root&password=root");
}catch (Exception e) {
System.out.println("e");
}
try{
st=con.createStatement();
rs=st.executeQuery("select * from clients");
while(rs.next()) {
System.out.println(rs.getString("Key"));
}
}
finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
}catch (SQLException e) {
System.out.println(e);
}
}
}
}
所有这些都会返回一个错误(在Eclipse中)
e
线程“main”java.lang.NullPointerException中的异常位于
main(JDBCTest.java:22)
我假设这是因为没有连接到数据库,所以con为null。。。但是为什么呢?如果将System.out.println(“e”)替换为e.printStackTrace(),则在创建连接时会出现详细的异常情况。
它可能是ClassNotFoundException。
并且,如果是密码不正确异常,则替换
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Testing1?user=root&password=root");
与
我已经尝试了你的代码,它对我来说很有用,只是在url中做了一些更改。获取连接时也会出现打印异常
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCTest{
private static Connection con = null;
private static Statement st = null;
private static ResultSet rs = null;
public static void main(String args[]) throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Testing1?user=root&password=root");
} catch (Exception e) {
System.out.println(e);
}
try {
st = con.createStatement();
rs = st.executeQuery("select * from users");
while (rs.next()) {
System.out.println(rs.getString("Key"));
}
} finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
System.out.println(e);
}
}
}
}
在用e.printStackTrace()替换System.out.println(“e”)之后,不幸的是,我仍然得到相同的错误。我的意思是,你可以得到详细的异常,这样我们就可以知道如何解决问题。嗯,我不知道你的意思。更换它没有任何区别。我是不是犯了一些愚蠢的新手错误?输出仍然是“e”?你重新编译了吗?是的,我现在发现了ClassNotFoundException。我将这个导出CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar添加到我的bash.bashrc文件中。这是错误的吗?这意味着没有像我这样的数据库,或者有其他错误吗?@Deerder只是在程序上面运行,让我们共享输出现在它显示:java.lang.ClassNotFoundException:com.mysql.jdbc。Driver@deerdeer所以现在你有问题了,在JDBCTest.main(JDBCTest.java:20)的线程“main”java.lang.NullPointerException中,您的classpathjava.lang.ClassNotFoundException:com.mysql.jdbc.Driver异常中没有jar
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCTest{
private static Connection con = null;
private static Statement st = null;
private static ResultSet rs = null;
public static void main(String args[]) throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Testing1?user=root&password=root");
} catch (Exception e) {
System.out.println(e);
}
try {
st = con.createStatement();
rs = st.executeQuery("select * from users");
while (rs.next()) {
System.out.println(rs.getString("Key"));
}
} finally {
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
System.out.println(e);
}
}
}
}