jsp中关于数据库的源码(jsp写入数据库)
本文目录一览:
jsp连接数据库和读数据源代码
楼上的回答没有错。
但是个人觉得既然是JSP,还是把数据库的连接写到后台,不写到jsp页面内。把数据库的单独写成一个类.
如下的两种连接方式:
1、
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
public class ConnectionManager {
/**
* driver class.
*/
private static final String DRIVER_CLASS =
"sun.jdbc.odbc.JdbcOdbcDriver";
/**
* database source.
*/
private static final String DATASOURCE = "jdbc:odbc:NetBarDataSource";
/**
* get connection.
* @return Connection
*/
public static Connection getConnction() {
Connection dbConnection = null;
try {
Class.forName(DRIVER_CLASS);
dbConnection = DriverManager.getConnection(DATASOURCE,"sa","");
} catch (Exception e) {
e.printStackTrace();
}
return dbConnection;
}
/**
* close a connection.
* @param dbConnection Connection
*/
public static void closeConnection(Connection dbConnection) {
try {
if (dbConnection != null (!dbConnection.isClosed())) {
dbConnection.close();
}
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
}
/**
* close the resuletset.
* @param res ResultSet
*/
public static void closeResultSet( ResultSet res) {
try {
if (res != null) {
res.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* close the statement.
* @param pStatement PreparedStatement
*/
public static void closeStatement( PreparedStatement pStatement) {
try {
if (pStatement != null) {
pStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2、
package jdbc2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
public class ConnectionManager {
/**
* driver class.
*/
/**
* database source.
*/
private static final String DRIVER_CLASS =
"com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String DATABASE_URL =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=NetBar";
private static final String DATABASE_USRE = "sa";
private static final String DATABASE_PASSWORD = "";
/**
* get connection.
* @return Connection
*/
public static Connection getConnction() {
Connection dbConnection = null;
try {
Class.forName(DRIVER_CLASS);
dbConnection = DriverManager.getConnection(DATABASE_URL, DATABASE_USRE,
DATABASE_PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return dbConnection;
}
/**
* close a connection.
* @param dbConnection Connection
*/
public static void closeConnection(Connection dbConnection) {
try {
if (dbConnection != null (!dbConnection.isClosed())) {
dbConnection.close();
}
} catch (SQLException sqlEx) {
sqlEx.printStackTrace();
}
}
/**
* close the resuletset.
* @param res ResultSet
*/
public static void closeResultSet( ResultSet res) {
try {
if (res != null) {
res.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* close the statement.
* @param pStatement PreparedStatement
*/
public static void closeStatement( PreparedStatement pStatement) {
try {
if (pStatement != null) {
pStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
方法1为桥连接方式,方法2是需要导入微软的JDBC驱动
用jsp查询本地sql数据库的信息源代码是?
不同的环境和代码,写法肯定不一样的,,,,,
主要是使用jdbc访问数据库——记得添加相应的驱动的.jar到CLASSPATH
~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~
小弟跪求JSP连接数据库的源代码,本人不是很懂~~~
package Connection;
import java.sql.*;
public class DB
{
public static Connection getConnection()
{
Connection con = null;
String CLASSFORNAME = "com.microsoft.jdbc.sqlserver.SQLServer";
String SERVANDDB = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=test";
String USER="sa";
String PWD = "1234";
try
{
Class.forName(CLASSFORNAME);
con = DriverManager.getConnection(SERVANDDB,USER,PWD);
}
catch(Exception e){e.printStackTrace();}
return con;
}
}
连接数据库代码
网页1,一个表单
%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%
%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
base href="%=basePath%"
titleMy JSP 'add.jsp' starting page/title
meta http-equiv="pragma" content="no-cache"
meta http-equiv="cache-control" content="no-cache"
meta http-equiv="expires" content="0"
meta http-equiv="keywords" content="keyword1,keyword2,keyword3"
meta http-equiv="description" content="This is my page"
!--
link rel="stylesheet" type="text/css" href="styles.css"
--
/head
body
form action = "add_do.jsp" method = "post" name = "emp"
table border="1" bgcolor="yellow"
trtdidinput type="text" name="id"/td/tr
trtdnameinput type="text" name="name"/td/tr
trtdageinput type="text" name="age"/td/tr
trtdinput type="submit" value="tj"/td/tr
/table
/form
/body
/html
网页2,
%@ page language="java" import="java.util.*,com.bean.*,com.GetSet.attribute.*,Connection.*" pageEncoding="UTF-8"%
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
html
head
titleMy JSP 'add_do.jsp' starting page/title
meta http-equiv="pragma" content="no-cache"
meta http-equiv="cache-control" content="no-cache"
meta http-equiv="expires" content="0"
meta http-equiv="keywords" content="keyword1,keyword2,keyword3"
meta http-equiv="description" content="This is my page"
!--
link rel="stylesheet" type="text/css" href="styles.css"
--
/head
jsp:useBean id = "emp" class="6ad2-a3db-fa4f-3d4d com.GetSet.attribute.Employee" scope="page"
jsp:setProperty name = "emp" property="*"/
/jsp:useBean
jsp:useBean id = "CB" class = "com.bean.ContactBean" scope="page"/
body
% CB.setUserInfo(emp);
CB.regist();
out.println("注册成功");
%
成功
/body
/html
这个是向数据库里插入数据,如果你还要其他的在反有分的问题啊~