日期:2014-05-16  浏览次数:20452 次

java手动连接JDBC
/**
* 手动连接jdbc数据库连接
*/
package com.bjsxt.jbpm;

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


public class Jdbc {

public static void main(String[] args) {

String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jdbc;User=sa;Password=sa";

try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
Connection conn = (Connection) DriverManager.getConnection(url);
Statement st = (Statement) conn.createStatement();

String sql = "";

/*sql = "insert into userinfo (name,sex,phone) values( 'Tomqq','1','1234457') ";

st.executeUpdate(sql);*/

sql ="select name from userinfo where name = 'tomqq'";

ResultSet rs = st.executeQuery(sql);

while(rs.next()){
System.out.println(rs.getString("name"));
}
st.close();
conn.close();

} catch (SQLException e) {

e.printStackTrace();
}
}

}// /:~