package com.honglian.xjFocus.web.service;
import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
public class Test {
/**
* @param args
*/
//获取连接
public Connection getConnection(){
Connection con=null;
String driver="com.mysql.jdbc.Driver";
String user="root";
String password="sa";
String url="jdbc:mysql://localhost:3306/car";
try {
Class.forName(driver);
con=(Connection)DriverManager.getConnection(url, user, password);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return con;
}
//执行插入操作
public void Insertsql(){
Connection con=null;
Test test=new Test();
con=test.getConnection();
try {
int cnt=10;
PreparedStatement pstmt=(PreparedStatement)con.prepareStatement("insert into tb_car values(?,?)");
for (int i = 0; i < cnt; i++) {
pstmt.setString(1,"car"+i);
pstmt.setString(2, "name"+i);
pstmt.executeUpdate();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//查询操作
public void selectsql(){
Connection con=null;
Test test=new Test();
con=test.getConnection();
try {
Statement stmt=(Statement)con.createStatement();
String sql="select * from tb_car";
ResultSet result=(ResultSet)stmt.executeQuery(sql);
System.out.println("id"+" "+"名字");
while(result.next()){
System.out.println(result.getString("id")+" "+result.getString("car_name"));
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
Connection conn = null;
Statement state = null;
ResultSet result = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/car?user=root&password=sa";
conn = (Connection) DriverManager.getConnection(url);
state = (Statement) conn.createStatement();
String sql = "select * from tb_car;";
result = (ResultSet) state.executeQuery(sql);
while(result.next()){
System.out.println(result.getString("id"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
result.close();
state.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}*/
Test test=new Test();
test.Insertsql();
test.selectsql();
}
}
import java.sql.DriverManager;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
public class Test {
/**
* @param args
*/
//获取连接
public Connection getConnection(){
Connection con=null;
String driver="com.mysql.jdbc.Driver";
String user="root";
String password="sa";
String url="jdbc:mysql://localhost:3306/car";
try {
Class.forName(driver);
con=(Connection)DriverManager.getConnection(url, user, password);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return con;
}
//执行插入操作
public void Insertsql(){
Connection con=null;
Test test=new Test();
con=test.getConnection();
try {
int cnt=10;
PreparedStatement pstmt=(PreparedStatement)con.prepareStatement("insert into tb_car values(?,?)");
for (int i = 0; i < cnt; i++) {
pstmt.setString(1,"car"+i);
pstmt.setString(2, "name"+i);
pstmt.executeUpdate();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//查询操作
public void selectsql(){
Connection con=null;
Test test=new Test();
con=test.getConnection();
try {
Statement stmt=(Statement)con.createStatement();
String sql="select * from tb_car";
ResultSet result=(ResultSet)stmt.executeQuery(sql);
System.out.println("id"+" "+"名字");
while(result.next()){
System.out.println(result.getString("id")+" "+result.getString("car_name"));
}
} catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
Connection conn = null;
Statement state = null;
ResultSet result = null;
try{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/car?user=root&password=sa";
conn = (Connection) DriverManager.getConnection(url);
state = (Statement) conn.createStatement();
String sql = "select * from tb_car;";
result = (ResultSet) state.executeQuery(sql);
while(result.next()){
System.out.println(result.getString("id"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
result.close();
state.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}*/
Test test=new Test();
test.Insertsql();
test.selectsql();
}
}