我正在尝试用Java连接到SQL Server数据库,执行查询,并将结果返回给调用脚本。
class DB
{
public DB() {}
public Connection dbConnect(String db_connect_string)
{
try
{
Class.forName(
"com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn =
DriverManager.getConnection(db_connect_string);
System.out.println("connected");
return conn;
}
catch (Exception e)
{
e.printStackTrace();
return true;
}
}
};
class getConnection
{
public void main(String[] args)
{
DB db = new DB();
Connection conn=db.dbConnect(
"jdbc:microsoft:sqlserver://host:1433;User=user;Password=Pwd");
}
}
return true;
public Connection dbConnect(String db_connect_string)
{
try
{
Class.forName(
"com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn =
DriverManager.getConnection(db_connect_string);
System.out.println("connected");
return conn;
}
catch (Exception e)
{
e.printStackTrace();
return true;
}
}
};
class getConnection
{
public void main(String[] args)
{
DB db = new DB();
Connection conn=db.dbConnect(
"jdbc:microsoft:sqlserver://host:1433;User=user;Password=Pwd");
}
}
return true;
据我所知,这告诉我连接正常,但我的Java不够有限。有人可以告诉我这部分是否正常并为其添加一个简单的select语句,然后通过return传回来?
答案 0 :(得分:2)
答案 1 :(得分:2)
您可能应该了解如何使用JDBC
public Statement getStatement()
{
return conObject.createStatement();
}