哪个知道Servlet中怎么连JDBC要详细点的
2个回答
展开全部
import java.sql.*;
public class JtdsDBConnection {
/**
* @param args
*/
public static void main(String[] args) {
String driverName = "net.sourceforge.jtds.jdbc.Driver";
String dbURL = "jdbc:jtds:sqlserver://172.16.15.28:1433/jforum";
String userName = "sa";
String userPwd = "12345678";
Connection con;
PreparedStatement stmt = null;
ResultSet rs = null;
String sql = "select * from jforum_posts_text";
try {
Class.forName(driverName);
con = DriverManager.getConnection(dbURL, userName, userPwd);
stmt = con.prepareStatement(sql);
rs = stmt.executeQuery();
while(rs.next())
{
System.out.println(rs.getInt("post_id"));
System.out.println(rs.getString("post_text"));
}
System.out.println("Connection Successful!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
注意:1.net.sourceforge.jtds.jdbc.Driver 需要自己去下个连接驱动。
请搜索 jtds.
2.如要在serverlet 中使用,请复制main()中相关内容(尽管不是很优雅)。
3.可以在serverlet 中调用以上bean, 推荐此做法.
public class JtdsDBConnection {
/**
* @param args
*/
public static void main(String[] args) {
String driverName = "net.sourceforge.jtds.jdbc.Driver";
String dbURL = "jdbc:jtds:sqlserver://172.16.15.28:1433/jforum";
String userName = "sa";
String userPwd = "12345678";
Connection con;
PreparedStatement stmt = null;
ResultSet rs = null;
String sql = "select * from jforum_posts_text";
try {
Class.forName(driverName);
con = DriverManager.getConnection(dbURL, userName, userPwd);
stmt = con.prepareStatement(sql);
rs = stmt.executeQuery();
while(rs.next())
{
System.out.println(rs.getInt("post_id"));
System.out.println(rs.getString("post_text"));
}
System.out.println("Connection Successful!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
注意:1.net.sourceforge.jtds.jdbc.Driver 需要自己去下个连接驱动。
请搜索 jtds.
2.如要在serverlet 中使用,请复制main()中相关内容(尽管不是很优雅)。
3.可以在serverlet 中调用以上bean, 推荐此做法.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询