NteBeans下JSP连接MySQL示例

10/25/2009来源:JSP技巧人气:9530

注意:所有小写有下划线的地方均要自己改,如user_name,table_value1......

 

 

 

<%@ page contentType="text/html; charset=gb2312" language="java"

import="java.sql.*"%>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<%

String server="localhost";

String dbname="user_database";                    //database名字

String user="user_name";                            //用户名

String pass="user_passWord";               //密码


String url ="jdbc:MySQL://"+server+"/"+dbname+"?user="+user+"&password="+pass;

Class.forName("com.mysql.jdbc.Driver").newInstance();

Connection conn = DriverManager.getConnection(url);


Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,

ResultSet.CONCUR_UPDATABLE);

String sql="select * from user_table";                     //table名字

ResultSet rs=stmt.executeQuery(sql);


while(rs.next()){

     out.PRint("table_value1");

     out.print(rs.getString("table_value1")+" table_value2");

     out.println(rs.getString("table_value2")+"<br>");

}

rs.close();

stmt.close();

conn.close();

%>
<%
     out.print("<br>");

     out.print("数据库操作成功,恭喜你");

     rs.close();

%>