import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class vb {
private Connection conn = null;
PreparedStatement statement = null;
// connect to MySQL
void connSQL() {
String url = "jdbc:mysql://localhost:3306/hello?characterEncoding=UTF-8";
String username = "root";
String password = "root"; // 加载驱动程序以连接数据库
try {
Class.forName("com.mysql.jdbc.Driver" );
conn = DriverManager.getConnection( url,username, password );
}
//捕获加载驱动程序异常
catch ( ClassNotFoundException cnfex ) {
System.err.println(
"装载 JDBC/ODBC 驱动程序失败。" );
cnfex.printStackTrace();
}
//捕获连接数据库异常
catch ( SQLException sqlex ) {
System.err.println( "无法连接数据库" );
sqlex.printStackTrace();
}
}
// disconnect to MySQL
void deconnSQL() {
try {
if (conn != null)
conn.close();
} catch (Exception e) {
System.out.println("关闭数据库问题 :");
e.printStackTrace();
}
}
// execute selection language
ResultSet selectSQL(String sql) {
ResultSet rs = null;
try {
statement = conn.prepareStatement(sql);
rs = statement.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
// execute insertion language
boolean insertSQL(String sql) {
try {
statement = conn.prepareStatement(sql);
statement.executeUpdate();
return true;
} catch (SQLException e) {
System.out.println("插入数据库时出错:");
e.printStackTrace();
} catch (Exception e) {
System.out.println("插入时出错:");
e.printStackTrace();
}
return false;
}
//execute delete language
boolean deleteSQL(String sql) {
try {
statement = conn.prepareStatement(sql);
statement.executeUpdate();
return true;
} catch (SQLException e) {
System.out.println("插入数据库时出错:");
e.printStackTrace();
} catch (Exception e) {
System.out.println("插入时出错:");
e.printStackTrace();
}
return false;
}
//execute update language
boolean updateSQL(String sql) {
try {
statement = conn.prepareStatement(sql);
statement.executeUpdate();
return true;
} catch (SQLException e) {
System.out.println("插入数据库时出错:");
e.printStackTrace();
} catch (Exception e) {
System.out.println("插入时出错:");
e.printStackTrace();
}
return false;
}
// show data in ju_users
void layoutStyle2(ResultSet rs) {
System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------");
System.out.println(" 用户ID" + "/t/t" + "淘宝ID" + "/t/t" + "用户名"+ "/t/t" + "密码");
System.out.println("-----------------");
try {
while (rs.next()) {
System.out.println(rs.getInt("ju_userID") + "/t/t"
+ rs.getString("taobaoID") + "/t/t"
+ rs.getString("ju_userName")
+ "/t/t"+ rs.getString("ju_userPWD"));
}
} catch (SQLException e) {
System.out.println("显示时数据库出错。");
e.printStackTrace();
} catch (Exception e) {
System.out.println("显示出错。");
e.printStackTrace();
}
}
public static void main(String args[]) {
vb h = new vb();
h.connSQL();
String s = "select * from ju_users";
String insert = "insert into ju_users(ju_userID,TaobaoID,ju_userName,ju_userPWD) values("+8329+","+34243+",'mm','789')";
String update = "update ju_users set ju_userPWD =123 where ju_userName= 'mm'";
String delete = "delete from ju_users where ju_userName= 'mm'";
if (h.insertSQL(insert) == true) {
System.out.println("insert successfully");
ResultSet resultSet = h.selectSQL(s);
h.layoutStyle2(resultSet);
}
if (h.updateSQL(update) == true) {
System.out.println("update successfully");
ResultSet resultSet = h.selectSQL(s);
h.layoutStyle2(resultSet);
}
if (h.insertSQL(delete) == true) {
System.out.println("delete successfully");
ResultSet resultSet = h.selectSQL(s);
h.layoutStyle2(resultSet);
}
h.deconnSQL();
}
}
无法连接数据库
插入时出错:
插入时出错:
插入时出错:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2103)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at vb.connSQL(vb.java:19)
at vb.main(vb.java:128)
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:244)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:253)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:280)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2026)
... 13 more
java.lang.NullPointerException
at vb.insertSQL(vb.java:60)
at vb.main(vb.java:135)
java.lang.NullPointerException
at vb.updateSQL(vb.java:90)
at vb.main(vb.java:140)
java.lang.NullPointerException
at vb.insertSQL(vb.java:60)
at vb.main(vb.java:145)
|
|