JDBC的连接问题!新手求指导。
import java.sql.DriverManager;
import java.sql.ResultSet;
import
java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
public class MysqlDemo {
public static void main(String[] args) throws Exception {
Connection conn = null;
String sql;
String url = ("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test");
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");// 动态加载mysql驱动
System.out.println("成功加载MySQL驱动程序");
// 一个Connection代表一个数据库连接
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test","sa","1234");
// Statement里面带有很多方法,比如executeUpdate可以实现插入,更新和删除等
Statement stmt = conn.createStatement();
sql = "create table student(NO char(20),name varchar(20),primary key(NO))";
int result = stmt.executeUpdate(sql);// executeUpdate语句会返回一个受影响的行数,如果返回-1就没有成功
if (result != -1) {
System.out.println("创建数据表成功");
sql = "insert into student(NO,name) values('2012001','陶伟基')";
result = stmt.executeUpdate(sql);
sql = "insert into student(NO,name) values('2012002','周小俊')";
result = stmt.executeUpdate(sql);
sql = "select * from student";
ResultSet rs = stmt.executeQuery(sql);// executeQuery会返回结果的集合,否则返回空值
System.out.println("学号\t姓名");
while (rs.next()) {
System.out
&