package com.kunpeng.jdbc;

import java.awt.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SQLHelper {

public String ip = "127.0.0.1";
public String port = "1433";
public String dataName = "master";
public String usrName = "sa"; //数据库用户名
public String password = "123456"; //数据库密码
public Connection conn = null; //连接对象变量
public Statement stmt = null; //陈述对象(跑腿用)
public ResultSet re = null; //存储查询结果

public SQLHelper(String ip, String port, String dataName, String usrName,
String password, Connection conn, Statement stmt) {
super();
this.ip = ip;
this.port = port;
this.dataName = dataName;
this.usrName = usrName;
this.password = password;
this.conn = conn;
this.stmt = stmt;

}

public SQLHelper() {
super();
// TODO Auto-generated constructor stub
}
/*
public static void main(String[] args) throws Exception{

String connStr = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=student;user=sa;password=123456"; //连接字符串
//String usrName = "sa"; //数据库用户名
//String password = "123456"; //数据库密码
//1.注册驱动,告诉软件要用哪个驱动.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2.创建连接,把软件与数据库之间连起来.
//Connection conn = DriverManager.getConnection(connStr,usrName,password);
Connection conn = DriverManager.getConnection(connStr);
System.out.println("连接数据库成功!");
//----------------------------------------------------------------------
//上面做的事只是连接到了数据库.下面我们试着在student库中创建一个表(test).
//create table test(id int primary key not null,name varchar(50))

//3.如果想操作数据库,必须要个跑腿的:Statement对象
Statement stmt = conn.createStatement();

//4.有了statement对象,则可以对数据库进行操作了,非查询用:executeUpdate()方法,查询用:executeQuery()
stmt.executeUpdate("drop table test");
stmt.executeUpdate("create table test(id int primary key not null,name varchar(50))");
stmt.executeUpdate("insert into test values(1,'0001')");
stmt.executeUpdate("insert into test values(2,'0002')");
stmt.executeUpdate("insert into test values(3,'0003')");

//5.数据库查询的结果 ResultSet
ResultSet re = stmt.executeQuery("select * from test");

while(re.next()){
System.out.println(re.getInt("id")+ "----" + re.getString("name"));
}
//----------------------------------------------------------------------
//最后一般要记得:手动关闭连接

stmt.close();
conn.close();
System.out.println("成功关闭数据库连接!");
}
*/
public void open() throws Exception{
String connStr = "jdbc:sqlserver://"+this.ip+":"+this.port+";DatabaseName="+this.dataName+""; //连接字符串
//1.注册驱动,告诉软件要用哪个驱动.
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2.创建连接,把软件与数据库之间连起来.
conn = DriverManager.getConnection(connStr,this.usrName,this.password);
//3.如果想操作数据库,必须要个跑腿的:Statement对象
stmt = conn.createStatement();
System.out.println("数据库打开成功!");
}
public void close() throws Exception{
this.stmt.close();
this.conn.close();

}
public void exec(String sqlStr) throws Exception{
stmt.executeUpdate(sqlStr);
}

public ResultSet select(String sqlStr) throws Exception{
return stmt.executeQuery(sqlStr);
}

}

===============================================

引用它

import com.kunpeng.jdbc.*;

public class Test {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
SQLHelper sql = new SQLHelper();
sql.open();
//123123123123

sql.exec("create table aaa123a(id int primary key,name varchar(50))");
sql.exec("create table aaa124a(id int primary key,name varchar(50))");
sql.exec("create table aaa125a(id int primary key,name varchar(50))");
sql.exec("create table aaa126a(id int primary key,name varchar(50))");
sql.close();

}

}