import java.sql.*
public class BaseDAO {
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"//注意:此驱动是SQL2005及以上版本的导入驱动包连接字符串
private static final String CONNECTION = "jdbc:sqlserver://localhost:1433databaseName=Employee"//数据库连接字符串,databaseName就是你要连接的数据库名,
private static final String NAME = "sa" //数据库用户名
private static final String PWD = "sa" //数据库密码
public static Connection GetConnection() {
Connection con = null
try {
Class.forName(DRIVER)
con = DriverManager.getConnection(CONNECTION, NAME, PWD)
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace()
}
return con
}
public static void close(ResultSet rs, PreparedStatement ps, Connection con) {
try {
if (null != rs) {
rs.close()
}
if (null != ps) {
ps.close()
}
if (null != con) {
con.close()
}
} catch (Exception e) {
e.printStackTrace()
}
}
}
import java.sql.*public class MSSQLText
{
public static void main(String args[])
{
String url="jdbc:microsoft:sqlserver://localhost:1433DatabaseName=Northwind"
String user="sa"//这里替换成你自已的数据库用户名
String password="sa"//这里替换成你自已的数据库用户密码
String sqlStr="select CustomerID, CompanyName, ContactName from Customers"
try
{ //这里的异常处理语句是必需的.否则不能通过编译!
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
System.out.println("类实例化成功!")
Connection con = DriverManager.getConnection(url,user,password)
System.out.println("创建连接对像成功!")
Statement st = con.createStatement()
System.out.println("创建Statement成功!")
ResultSet rs = st.executeQuery(sqlStr)
System.out.println(" *** 作数据表成功!")
System.out.println("----------------!")
while(rs.next())
{
System.out.print(rs.getString("CustomerID") + "")
System.out.print(rs.getString("CompanyName") + "")
System.out.println(rs.getString("ContactName"))
}
rs.close()
st.close()
con.close()
}
catch(Exception err){
err.printStackTrace(System.out)
}
}
}
rowVector.add(String.valueOf(rs.getString(textField.getText())))这里有问题,rs.getString(textField.getText())))textField.getText()你这里应该改成对应你要获得的列名哦。而不是你输入的值。欢迎分享,转载请注明来源:内存溢出
评论列表(0条)