import java.io.BufferedReader;
import java.io.InputStreamReader;public class demo {
/**
* @param args
*/
public static void main(String[] args) {
// 数据库IP 192.168.1.131
// 用户名 sa 密码 accp
String bats = "test1,test2,test3";
// 获取bat文件路径
String batpath = System.getProperty("user.dir");
batpath = batpath + "\\bat\\";
// 执行bat文件
String[] bats2 = bats.split(",");
for (int i = 0; i < bats2.length; i++) {
Runtime temprun = Runtime.getRuntime();
Process tempexe;
try {
tempexe = temprun.exec(batpath + "test3.bat");
tempexe = temprun.exec(batpath + bats2[i] + ".bat");
BufferedReader tempbr = new BufferedReader(
new InputStreamReader(tempexe.getInputStream()));
String tempLine;
while ((tempLine = tempbr.readLine()) != null) {
System.out.println(tempLine);
}
// 执行成功
System.out.println("Connection Successful!");
System.out.println("执行成功值为");
} catch (Exception e) {
//TODO Auto-generated catch block
// 执行失败,
System.out.println("执行失败");
e.printStackTrace();
}
}
}}
这段代码要用什么样的方法才能连接到数据库SQLserver2005?并且能显示执行成功和执行失败的状态?成功的话能给予赋值,失败则返回,本人对于这个JDBC连接数据库是不是很了解所以在这里求教了!!!
import java.io.InputStreamReader;public class demo {
/**
* @param args
*/
public static void main(String[] args) {
// 数据库IP 192.168.1.131
// 用户名 sa 密码 accp
String bats = "test1,test2,test3";
// 获取bat文件路径
String batpath = System.getProperty("user.dir");
batpath = batpath + "\\bat\\";
// 执行bat文件
String[] bats2 = bats.split(",");
for (int i = 0; i < bats2.length; i++) {
Runtime temprun = Runtime.getRuntime();
Process tempexe;
try {
tempexe = temprun.exec(batpath + "test3.bat");
tempexe = temprun.exec(batpath + bats2[i] + ".bat");
BufferedReader tempbr = new BufferedReader(
new InputStreamReader(tempexe.getInputStream()));
String tempLine;
while ((tempLine = tempbr.readLine()) != null) {
System.out.println(tempLine);
}
// 执行成功
System.out.println("Connection Successful!");
System.out.println("执行成功值为");
} catch (Exception e) {
//TODO Auto-generated catch block
// 执行失败,
System.out.println("执行失败");
e.printStackTrace();
}
}
}}
这段代码要用什么样的方法才能连接到数据库SQLserver2005?并且能显示执行成功和执行失败的状态?成功的话能给予赋值,失败则返回,本人对于这个JDBC连接数据库是不是很了解所以在这里求教了!!!
语句
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //加载驱动,反射
Connection dbConn=DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=DBDesign","sa", "sa");
//上面这个就是连接到数据库,方法有三个参数,第一个是URL,第二个用户名,第三个密码
//记得用try catch括起来
至于bat究竟干了啥,你所贴代码是看不出来的。
你究竟需要什么效果?如果是用JDBC访问SQLServer的话,网上Google大把样例。如果是借助bat去操作SQLServer的话,那是另一回事了,要看你的bat究竟干了什么了。