插入数据 import java.sql.*; /** * @version 2012-02-22 * @author */ public class InsertDemo { public static void main(String[] args) throws SQLException { Connection conn = null; Statement stmt = null; String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL鏁版嵁搴撳紩鎿� String connectDB="jdbc:sqlserver://localhost:1433;DatabaseName=JDBCTest";//鏁版嵁婧惵� String user="sa"; String pwd="sql"; try { // 动态导入数据库的驱动 Class.forName(JDriver); // 获取数据库链接 conn = DriverManager.getConnection(connectDB,user,pwd); // 创造SQL语句 String sql = "INSERT INTO JDBCTestTable ( user_name, user_password ) VALUES ( 'balabala', '123456' )"; // 执行SQL语句 stmt = conn.createStatement(); stmt.executeUpdate(sql); System.out.println("插入数据成功"); } catch (Exception e) { e.printStackTrace(); stmt.close(); conn.close(); } } } 修改数据 import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class UpdateDemo { public static void main(String[] args) throws SQLException { Connection conn = null; Statement stmt = null; try { // 动态导入数据库的驱动 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); // 获取数据库链接 conn = DriverManager.getConnection( "jdbc:sqlserver://localhost:1433;DatabaseName=JDBCTest", "sa", "sql"); // 创造SQL语句 String sql = "UPDATE JDBCTestTable SET user_password = '456' WHERE user_name = 'Eric'"; // 执行SQL语句 stmt = conn.createStatement(); stmt.executeUpdate(sql); System.out.println("更新数据成功"); } catch (Exception e) { e.printStackTrace(); stmt.close(); conn.close(); } } } 删除数据 import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class DeleteDemo { public static void main(String[] args){ String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=JDBCTest"; String user="sa"; String password="sql"; try{ Class.forName(JDriver);// 动态导入数据库的驱动 Connection conn=DriverManager.getConnection(url, user, password);// 获取数据库链接 String query="DELETE FROM JDBCTestTable WHERE user_name='Eric'";// 创造SQL语句 Statement stmt=conn.createStatement();// 执行SQL语句 stmt.executeUpdate(query); System.out.println("删除数据成功"); stmt.close(); conn.close(); }catch(Exception e){ e.printStackTrace(); } } } 查询数据 import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class QueryDemo { public static void main(String[] args){ String JDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=JDBCTest"; String user="sa"; String password="sql"; try{ Class.forName(JDriver);// 动态导入数据库的驱动 Connection conn=DriverManager.getConnection(url, user, password);// 获取数据库链接 String query="SELECT * FROM JDBCTestTable";// 创造SQL语句 Statement stmt=conn.createStatement();// 执行SQL语句 ResultSet rs=stmt.executeQuery(query); while(rs.next()){ System.out.println(rs.getString("user_name")+":"+rs.getString(2)); //密码字段的编号从1开始,密码排第二位 } System.out.println("查询数据成功"); rs.close(); stmt.close(); conn.close(); }catch(Exception e){ e.printStackTrace(); } } }