首页 » ASP代码 » JSP连接MYSQL的代码

JSP连接MYSQL的代码

Connection con;
Statement stmt;
ResultSet rs;
//加载驱动程序,下面的代码为加载MySQL驱动程序
Class.forName("com.mysql.jdbc.Driver");
//注册MySQL驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//用适当的驱动程序连接到数据库
String dbUrl = "jdbc:mysql://localhost:3306/BookDB?useUnicode=true&characterEncoding=UTF-8";
 
String dbUser="root";
String dbPwd="1234";
//建立数据库连接
con = java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd);
//创建一个JDBC声明
stmt = con.createStatement();
//增加新记录
stmt.executeUpdate("INSERT INTO books (id,name,title,price) VALUES ('999','Tom','Tomcat Bible',44.5)");
//查询记录
rs = stmt.executeQuery("SELECT id,name,title,price from books");

转发到新浪微博 转发到新浪微博

目前这篇文章有1条评论(Rss)

  1. Wyglada | #1
    05/17/2012 at 06:57

    I like the valuable info you provide in your articles. I’ll bookmark your blog and check again here frequently. I am quite certain I’ll learn a lot of new stuff right here! Good luck for the next!

我要评论