日期:2014-05-17 浏览次数:20688 次
package pratice.web;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ContectSQL{
public static Connection getConnection() throws SQLException{
try {
Class.forName(driverName).newInstance();
conn=DriverManager.getConnection(url,user,password);
}
catch (Exception e) {
e.printStackTrace();
}
return conn;
}
private static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Pratice";
private static String user="sa";
private static String password="";
private static Connection conn=null;
}
<%@ page language="java" contentType="text/html; charset=utf-8" import="java.sql.*"%>
<jsp:useBean id="ConString" scope="page" class="pratice.web.ContectSQL"></jsp:useBean>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
try{
Connection conn=ConString.getConnection();
PreparedStatement stmt=conn.prepareStatement("insert into UserInfo(UserID,UserName,Age,Address,Phone,Email) values(?,?,?,?,?,?)");
stmt.setInt(1, 6);
stmt.setString(2, "abc");
stmt.setInt(3, 21);
stmt.setString(4, "山东");
stmt.setString(5, "123456");
stmt.setString(6, "abc@gmail.com");
stmt.executeUpdate();
stmt.close();
}
catch(Exception e)
{
out.println(e);
}