jsp学习--jdbc数据库连接
import java.sql.*;public class SqlConnect {private static Object Results;public static void main(String[] args) {//驱动注册try {Class.forName("org.apache.derby.jdbc.ClientDriver");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}//链接数据库Connection con = null; //数据库链接Statement smt = null; //数据库表达式ResultSet rst = null; //结果集//连接数据库try {con = java.sql.DriverManager.getConnection("jdbc:derby://localhost:1527/myeclipse","classiccars","classiccars");//数据库操作/添加/删除smt = con.createStatement(); rst = smt.executeQuery("select * from student");//查里面的数据while (rst != null && rst.next()){System.out.println("学生编号"+rst.getString("id") );//表示一行中的第一列。getString字段。123一行中的第一列第二列System.out.println("学生姓名:"+rst.getString(2));//表示一行中的第二列。getString("username")System.out.println("password"+rst.getString("password"));}int rows = smt.executeUpdate("insert into student(username,password) values('zufe','z')");System.out.println(rows);int rows2 = smt.executeUpdate("deletefrom student where id=1");System.out.println("插入了"+rows2);}catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {con.close();} catch (Exception e1) {// TODO Auto-generated catch blocke1.printStackTrace();}try {smt.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}try {rst.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
页:
[1]