jsp数据库链接代码
<%@ page contentType=”text/html;charset=utf-8″ %> <%@ page import=”java.sql.*” %> <%@ page import=”com.mysql.jdbc.Driver” %> <% //驱动器程序名 String driverName = “com.mysql.jdbc.Driver”; //数据库名称 String userName = “root”; //密码 String userPasswd = “123456″; //数据库名称 String dbName = “1270011″; //表名 String tableName = “dbl_menu”; //联结字符串 String url = “jdbc:mysql://localhost/”+dbName; Class.forName(driverName).newInstance(); Connection connection = DriverManager.getConnection(url,userName,userPasswd); Statement statement = connection.createStatement(); String sql = “select * from “+tableName; ResultSet rs = statement.executeQuery(sql); //获取数据结果集合 //ResultSetMetaData rmeta = rs.getMetaData(); //确定数据集列数,即字段数 //int numColumns = rmeta.getColumnCount(); //输入每个数据值 out.println(“UserName:”); while(rs.next()){ out.println(rs.getString(4)+”<br />”);//底数从1开始 } rs.close(); //String update = “update dbl_menu set status=0″; //statement.executeUpdate(update); statement.close(); connection.close(); %>