运行内容如下的UseDriver.java文件,
import java.sql.*;
public class UseDriver {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost/sql_test";
String userName = "root";
String password = " ";
String sql = null;
Connection conn = null;
Statement stmt = null;
try {
//第一步:加载驱动器
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
System.err.print("ClassNotFoundException");
}
try {
//第二步:调用DriverManager.getConnection静态方法得到数据库连接
conn = DriverManager.getConnection(url, userName, password);
//创建Statement语句
stmt = conn.createStatement();
sql = "INSERT INTO student " +
"VALUES('12', 'zhangjun', 'tianjin', '1981-01-01')";
//使用Statement语句对象执行SQL语句
stmt.executeUpdate("DELETE FROM student WHERE stu_id='12'");
stmt.executeUpdate(sql);
System.out.println("Insert a row successful!");
} catch(SQLException e) {
System.err.println("Insert SQLException");
} finally {
//关闭语句和数据库连接
try {
stmt.close();
conn.close();
} catch(SQLException e) {
System.err.println("Close SQLException");
}
}
}
}在DOS窗体下运行,不知怎么出现如下的错误,请问一下这可能是什么错误啊,谢谢!
G:\>javac UseDriver.javaG:\>java UseDriver
Insert SQLException
Exception in thread "main" java.lang.NullPointerException
at UseDriver.main(UseDriver.java:35)
import java.sql.*;
public class UseDriver {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost/sql_test";
String userName = "root";
String password = " ";
String sql = null;
Connection conn = null;
Statement stmt = null;
try {
//第一步:加载驱动器
Class.forName("com.mysql.jdbc.Driver");
} catch(ClassNotFoundException e) {
System.err.print("ClassNotFoundException");
}
try {
//第二步:调用DriverManager.getConnection静态方法得到数据库连接
conn = DriverManager.getConnection(url, userName, password);
//创建Statement语句
stmt = conn.createStatement();
sql = "INSERT INTO student " +
"VALUES('12', 'zhangjun', 'tianjin', '1981-01-01')";
//使用Statement语句对象执行SQL语句
stmt.executeUpdate("DELETE FROM student WHERE stu_id='12'");
stmt.executeUpdate(sql);
System.out.println("Insert a row successful!");
} catch(SQLException e) {
System.err.println("Insert SQLException");
} finally {
//关闭语句和数据库连接
try {
stmt.close();
conn.close();
} catch(SQLException e) {
System.err.println("Close SQLException");
}
}
}
}在DOS窗体下运行,不知怎么出现如下的错误,请问一下这可能是什么错误啊,谢谢!
G:\>javac UseDriver.javaG:\>java UseDriver
Insert SQLException
Exception in thread "main" java.lang.NullPointerException
at UseDriver.main(UseDriver.java:35)
解决方案 »
- java web
- 求教高手:涉及数据结构(树)
- JSP笔记:forward和include方法的区别
- 想把一个页面里的几个form一个一个提交给不同的action怎么做到?
- 请问用sun标准的xml开发包如何创建出这样文件头(<?xml version="1.0" encoding="GBK"?>)的输出呢?
- 在struts中jsp页面取数据库值
- struts 2 部分规避"避免重复提交功能"
- 用JSP上传图片时,自动生成该图的缩图代码?
- 将resin添加为2000的服务之后出问题了,现在该怎么办?
- 如何在浏览器中打印本地文件
- 对JSP的迷茫
- 如何在不知道执行的SQL语句的情况下,显示出ResultSet中所有记录?
===
密码是一个空格?
stmt.executeUpdate(sql);
System.out.println("Insert a row successful!");
} catch(SQLException e) {
e.printStch....();//这里测试一下.看看具体错在哪里了
System.err.println("Insert SQLException");
} finally {
catch(SQLException e) {
System.err.println(e.getMessage());
System.err.println("Insert SQLException");显示的错误如下,请问一下这是什么错误:(mysql数据库我没设密码和用户名,所以用了root用户和空的密码)
G:\>javac UseDriver.javaG:\>java UseDriver
Access denied for user 'root'@'localhost' (using password: YES)
Insert SQLException
Exception in thread "main" java.lang.NullPointerException
at UseDriver.main(UseDriver.java:36)
grant all privileges on sql_test.* to test@localhost identified by “123456”;
新建一个用户和密码,
然后UseDriver.java文件再改一下:
String userName = "test";
String password = "123456";
就这样,搞定了!