小白才开始学JBDC 这是网上照着写的代码……
出现了
Exception in thread "main" java.lang.NullPointerException
at FirstExample.main(FirstExample.java:23)
空指针异常
但是不知道怎么改,求解。谢谢了
import java.sql.*;
public class FirstExample {
//JDBC driver name and databaase URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/EMP";
//Database credentials
static final String USER = "root";
static final String PASS = "2573718";
public static void main(String args[]) throws ClassNotFoundException{
Connection conn = null;
Statement stmt = null;
try{
//注册 JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//打开 a connection
System.out.println("Connecting to database..");
stmt = conn.createStatement();
String sql;
sql= "SELECT s_no FROM student";
ResultSet rs = stmt.executeQuery(sql);
//插入数据
while(rs.next())
{
int no = rs.getInt("s_no");
String name = rs.getString("s_name");
System.out.println("NO"+no);
System.out.println("Name"+name);
}
rs.close();
stmt.close();
conn.close();
}
catch(SQLException se){
se.printStackTrace();
}
finally{
try{
if(stmt!=null)
stmt.close();
}
catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}
catch(SQLException se){
se.printStackTrace();
}
}
}
}
你的数据库可能有问题,里边的字符不对
http://blog.csdn.net/ljheee/article/details/50988796
http://blog.csdn.net/ljheee/article/details/50752903
连接数据库的jar包加了吗?先写一个测试类看能不能连接上数据库。还有他不是提示了java类代码23行出错吗,而且这是插入数据吗?我怎么看不懂
首先,看驱动的jar导入没有,然后再检查连接驱动的参数是不是拼写正确了,给你的代码显示行数,提示说23有问题,看看这个地方
额 找到了 是数据库的名称错了………… 谢谢大家