import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBC {
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn = null;
try {
String url = "jdbc:mysql://localhost:3306/mysql";
//user:登录数据库的用户名
String user = "root"; //自己的账号
//password:用户名对应的密码,这些都是自己之前设定的
String password = "1234"; //自己的密码
//mySql的驱动:com.mysql.jdbc.Driver
String driverName = "com.mysql.jdbc.Driver";
//2.实例化Driver
Class clazz = Class.forName(driverName);
Driver driver = (Driver) clazz.newInstance();
//3.通过DriverManager来注册驱动
DriverManager.registerDriver(driver);
//4.通过DriverManager的getConnection方法,获取Connection类的对象
conn = DriverManager.getConnection(url, user, password);
selectTable(conn);
String insert_sql = "insert into student values('scofield',45,89,100)";
PreparedStatement preparedStatement = conn.prepareStatement(insert_sql);
int row = preparedStatement.executeUpdate(insert_sql);
if (row==1) {
System.out.println("insert successfully!");
selectTable(conn);
}
else if(row==0) {
System.out.println("insert unsuccessfully!");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
//当conn不为空时
if(conn != null)
//关闭conn资源
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void selectTable(Connection conn) throws SQLException {
System.out.println("select student:");
String sql = "select * from student";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
//执行语句
/**/
ResultSet resultSet = preparedStatement.executeQuery(sql);
//返回地数据存储在result中
System.out.print("[");
System.out.print("name\t");
System.out.print("English\t");
System.out.print("Math\t");
System.out.print("Computer");
System.out.println("]");
while (resultSet.next()){
System.out.print("[");
System.out.print( resultSet.getString("name")+"\t");
System.out.print( resultSet.getInt("English")+"\t");
System.out.print( resultSet.getInt("Math")+"\t");
System.out.print( resultSet.getInt("Computer")+"\t");
System.out.println("]");
}
if (resultSet != null){
resultSet.close();
}
if(preparedStatement != null){
preparedStatement.close();
}
}
}
(2)
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBC {
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection conn = null;
try {
String url = "jdbc:mysql://localhost:3306/mysql";
//user:登录数据库的用户名
String user = "root"; //自己的账号
//password:用户名对应的密码,这些都是自己之前设定的
String password = "1234"; //自己的密码
//mySql的驱动:com.mysql.jdbc.Driver
String driverName = "com.mysql.jdbc.Driver";
//2.实例化Driver
Class clazz = Class.forName(driverName);
Driver driver = (Driver) clazz.newInstance();
//3.通过DriverManager来注册驱动
DriverManager.registerDriver(driver);
//4.通过DriverManager的getConnection方法,获取Connection类的对象
conn = DriverManager.getConnection(url, user, password);
selectTable(conn);
// String insert_sql = "insert into student values('scofield',45,89,100)";
// PreparedStatement preparedStatement = conn.prepareStatement(insert_sql);
// int row = preparedStatement.executeUpdate(insert_sql);
// if (row==1) {
// System.out.println("insert successfully!");
// selectTable(conn);
// }
// else if(row==0) {
// System.out.println("insert unsuccessfully!");
// }
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
//当conn不为空时
if(conn != null)
//关闭conn资源
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void selectTable(Connection conn) throws SQLException {
System.out.println("select student:");
// String sql = "select * from student";
String sql = "select name,English from student where name='scofield'";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
//执行语句
/**/
ResultSet resultSet = preparedStatement.executeQuery(sql);
//返回地数据存储在result中
System.out.print("[");
System.out.print("name\t");
System.out.print("English\t");
// System.out.print("Math\t");
// System.out.print("Computer");
System.out.println("]");
while (resultSet.next()){
System.out.print("[");
System.out.print( resultSet.getString("name")+"\t");
System.out.print( resultSet.getInt("English")+"\t");
// System.out.print( resultSet.getInt("Math")+"\t");
// System.out.print( resultSet.getInt("Computer")+"\t");
System.out.println("]");
}
if (resultSet != null){
resultSet.close();
}
if(preparedStatement != null){
preparedStatement.close();
}
}
}