JSP中连接MySQL数据库
连接方法:
1.下载MySQL的JDBC驱动程序,拷贝到Tomcat安装目录的lib文件夹下;
2.驱动程序:org.gjt.mm.mysql.Driver
3.连接地址:jdbc:mysql://localhost/数据库名
实例:
jdbc_mysql.jsp:
view plaincopy to clipboardprint?
1. <%@ page contentType="text/html;charset=GB2312" %>
2. <%@ page import="java.sql.*" %>
3. <html>
4. <head>
5. <title>jdbc_mysql</title>
6. </head>
7.
8. <body>
9. <%!
10. String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
11. String DBURL = "jdbc:mysql://localhost/mgc" ;
12. String DBUSER = "root" ;
13. String DBPASSWORD = "123456" ;
14. Connection conn = null ;
15. Statement stmt = null ;
16. String sql = null ;
17. %>
18. <%
19. try {
20.
21. Class.forName(DBDRIVER) ;
22. conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
23. stmt = conn.createStatement();
24. sql = "Insert INTO member (id,username,password) VALUES(10,'qwe','qwe')" ;
25. stmt.executeUpdate(sql) ;
26. stmt.close() ;
27. conn.close() ;
28. }
29. catch(Exception e) {
30.
31. System.out.println(e) ;
32. }
33. %>
34. </body>
35. </html>
连接方法:
1.下载MySQL的JDBC驱动程序,拷贝到Tomcat安装目录的lib文件夹下;
2.驱动程序:org.gjt.mm.mysql.Driver
3.连接地址:jdbc:mysql://localhost/数据库名
实例:
jdbc_mysql.jsp:
view plaincopy to clipboardprint?
1. <%@ page contentType="text/html;charset=GB2312" %>
2. <%@ page import="java.sql.*" %>
3. <html>
4. <head>
5. <title>jdbc_mysql</title>
6. </head>
7.
8. <body>
9. <%!
10. String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
11. String DBURL = "jdbc:mysql://localhost/mgc" ;
12. String DBUSER = "root" ;
13. String DBPASSWORD = "123456" ;
14. Connection conn = null ;
15. Statement stmt = null ;
16. String sql = null ;
17. %>
18. <%
19. try {
20.
21. Class.forName(DBDRIVER) ;
22. conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
23. stmt = conn.createStatement();
24. sql = "Insert INTO member (id,username,password) VALUES(10,'qwe','qwe')" ;
25. stmt.executeUpdate(sql) ;
26. stmt.close() ;
27. conn.close() ;
28. }
29. catch(Exception e) {
30.
31. System.out.println(e) ;
32. }
33. %>
34. </body>
35. </html>