查询代码
package com.wj.demo01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
public class Demo3 {
@Test
public void query() {
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try {
conn=JDBCUtil.getConnection();
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from student");
while(rs.next()) {
int id=rs.getInt("id");
String name=rs.getString("name");
String city=rs.getString("city");
int age=rs.getInt("age");
System.out.println(id+":"+name+":"+city+":"+age);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtil.release(stmt, conn, rs);
}
}
@Test
public void delete() {
Connection conn=null;
Statement stmt=null;
try {
conn=JDBCUtil.getConnection();
stmt=conn.createStatement();
String sql="delete from student where id=11";
int count=stmt.executeUpdate(sql);
System.out.println("删除的行数是"+count);
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtil.release(stmt, conn);
}
}
@Test
public void update() {
Connection conn=null;
Statement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mydb3";
String user="root";
String password="1234";
conn=DriverManager.getConnection(url, user, password);
stmt=conn.createStatement();
String sql="update student set name='开心',city='广州' where id=11 ";
int count=stmt.executeUpdate(sql);
System.out.println("受影响的行数"+count);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(stmt!=null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Test
public void insert() {
Connection conn=null;
Statement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/mydb3";
String user="root";
String password="1234";
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
String sql="insert into student values(null,'开心','广州',18)";
int count=stmt.executeUpdate(sql);
System.out.println("受影响的行数是"+count);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(stmt!=null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
工具包
package com.wj.demo01;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtil {
private static String url=null;
private static String user=null;
private static String password=null;
static {
try {
Properties prop=new Properties();
InputStream is=JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
prop.load(is);
String driver=prop.getProperty("dirver");
Class.forName(driver);
url=prop.getProperty("url");
user=prop.getProperty("user");
password=prop.getProperty("password");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public static void release(Statement stmt,Connection conn) {
try {
if(stmt!=null) {
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void release(Statement stmt,Connection conn,ResultSet rs) {
try {
if(null!=rs) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
release(stmt, conn);
}
}
在scr下创建的文件
dirver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb3
user=root
password=1234