首页 > 编程 > JSP > 正文

NteBeans下JSP连接MySQL示例

2019-11-17 04:10:00
字体:
来源:转载
供稿:网友

注意:所有小写有下划线的地方均要自己改,如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();

%>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表