这是连接SQL Server2017数据库主要部分的代码,是在jsp文件中内嵌的连接代码,代码部分是没有问题的:
try{//载入JDBC驱动程序
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(ClassNotFoundException e)
{
e.printStackTrace();
}
try{//定义数据库连接的URL,数据库的名称是mylib
String url="jdbc:sqlserver://localhost:1433;DatabaseName=mylib";
//定义连接数据库的用户名和密码
String username="sa";
String password="123456";
//创建数据库连接
Connection conn=DriverManager.getConnection(url,username,password);
//创建Statement实例
Statement stmt=conn.createStatement();
request.setCharacterEncoding("gb2312");
String id=request.getParameter("bookID");//获取图书编号
String strSql="";
ResultSet rs=null;
if(id!="")//查询符合条件的记录
{
strSql="select bookID,bookName,bookAuth,bookPub from book where bookID='"+id+"'";
}
else//如果查询条件为空
版权声明:本文为znx970902原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。