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 block
e.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("delete from student where id=1");
System.out.println("插入了"+rows2);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
con.close();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
smt.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
rst.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
jsp学习--jdbc数据库连接
最新推荐文章于 2022-03-29 18:34:06 发布