数据库I/O连接不了
答案:2 悬赏:20 手机版
解决时间 2021-11-11 13:05
- 提问者网友:兔牙战士
- 2021-11-11 03:02
数据库I/O连接不了
最佳答案
- 五星知识达人网友:胯下狙击手
- 2021-11-11 03:25
给你这段代码看看 import java.sql.*;
import java.net.*;
public class sqltry { public static void main (String[] args)
{ String sDBDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test;user=lysander;password=";
Connection dbconn=null; try {
Class.forName(sDBDriver); dbconn=DriverManager.getConnection(dbURL); System.out.println("Connection Successful!"); sqltry.executeUpdateStatement(dbconn); }
catch (Exception e)
{ e.printStackTrace(); } } public static void executeUpdateStatement(Connection con)
{
try
{
String SQL = "SELECT * FROM [test].[dbo].[学校代码表] ORDER BY 最低分数";
String SQL2 ="INSERT INTO [test].[dbo].[学校代码表]([学校代码],[学校名],[招生人数],[最低分数]) VALUES (001,ybo1,2000,469)";
Statement stmt = con.createStatement();
int count = stmt.executeUpdate(SQL2);
//关了Statement
stmt.close();
System.out.println("use test datebase "+"ROWS AFFECTED: " + count);
//再建一Statement,下边的ResultSet用这个得到
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(SQL);
while(rs.next())
{
System.out.println(rs.getString("学校名") + ", " + rs.getString("学校代码")); }
//关闭后来建的Statement
st.close();
rs.close();
}
catch (Exception e)
{e.printStackTrace();} }
//public sqltry()
//{
//}
}
import java.net.*;
public class sqltry { public static void main (String[] args)
{ String sDBDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String dbURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test;user=lysander;password=";
Connection dbconn=null; try {
Class.forName(sDBDriver); dbconn=DriverManager.getConnection(dbURL); System.out.println("Connection Successful!"); sqltry.executeUpdateStatement(dbconn); }
catch (Exception e)
{ e.printStackTrace(); } } public static void executeUpdateStatement(Connection con)
{
try
{
String SQL = "SELECT * FROM [test].[dbo].[学校代码表] ORDER BY 最低分数";
String SQL2 ="INSERT INTO [test].[dbo].[学校代码表]([学校代码],[学校名],[招生人数],[最低分数]) VALUES (001,ybo1,2000,469)";
Statement stmt = con.createStatement();
int count = stmt.executeUpdate(SQL2);
//关了Statement
stmt.close();
System.out.println("use test datebase "+"ROWS AFFECTED: " + count);
//再建一Statement,下边的ResultSet用这个得到
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(SQL);
while(rs.next())
{
System.out.println(rs.getString("学校名") + ", " + rs.getString("学校代码")); }
//关闭后来建的Statement
st.close();
rs.close();
}
catch (Exception e)
{e.printStackTrace();} }
//public sqltry()
//{
//}
}
全部回答
- 1楼网友:独钓一江月
- 2021-11-11 03:56
为什么呢?
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯