java用sqljdbc4.jar链接sql 2005。。怎么样关联数据库?
发布网友
发布时间:2022-04-30 12:06
我来回答
共4个回答
热心网友
时间:2022-04-12 15:52
把你的sqljdbc4.jar放到classpath下
import java.sql.*;public class Test {
public static void main(String[] srg) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=test"; //连接服务器和数据库test
String userName = "sa"; //默认用户名
String userPwd = "123456"; //密码
Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful!
} catch (Exception e) {
e.printStackTrace();
}
}
}
热心网友
时间:2022-04-12 17:10
是TOMCAT服务吗?可以在context.xml中配置,如:
<Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
maxIdle="10"
maxWait="5000"
username="sa"
password=""
url="jdbc:sqlserver://localhost:1433;DatabaseName=mydb;"
maxActive="200"/>
其中“mydb”是你数据库的名称。追问我试普通的java链接。不用tomcat。
热心网友
时间:2022-04-12 18:44
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectSqlServer {
public static void main(String[] args) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=mydb";
String userName = "userName";
String userPwd = "password";
Connection dbConn;
Statement stmt;
ResultSet rs;
try {
// Class.forName("net.sourceforge.jtds.jdbc.Driver");
Class.forName(driverName);
// dbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://127.0.0.1:1433/mydb",userName,userPwd) ;
dbConn = DriverManager.getConnection(dbURL,userName,userPwd) ;
String sql = "Select name from tableName";
stmt = dbConn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
希望对你有所帮助,如果不行请参照下面的网址:
http://zhidao.baidu.com/question/353961991.html?oldq=1追问我的问题是..驱动有了。。怎么样用驱动连接到对应的数据库:?
追答不好意思,回复晚了。如果你是本地数据库,将【数据库名】换成你的数据库即可。
如果是远程的数据库,除了以上步骤,【localhost】换成相应的IP地址。
希望能帮到你。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectSqlServer {
public static void main(String[] args) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=数据库名";
String userName = "sa";
String userPwd = "sa";
Connection dbConn;
Statement stmt;
ResultSet rs;
try {
// Class.forName("net.sourceforge.jtds.jdbc.Driver");
Class.forName(driverName);
//dbConn = DriverManager.getConnection("jdbc:jtds:sqlserver://127.0.0.1:1433/TEST",userName,userPwd) ;
dbConn = DriverManager.getConnection(dbURL,userName,userPwd) ;
String sql = "Select * From tbl_user";
stmt = dbConn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getString("userName") + ", " + rs.getString("userPassword"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
热心网友
时间:2022-04-12 20:36
private List<Integer> list = null;
private int index = 0;
public Storage() {
list = new ArrayList<Integer>();
}
public synchronized void setNum(int num) {
list.add(num);
}
public synchronized int GetNum() {
if (index == list.size()) {
return -1;
}
return list.get(index++);
}