/*在进行下面的操作时,先配置好数据库连接驱动*/
//将数据插入oracle
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.1.224:1521:ora9";
String username = "scott";
String password = "root";
String sql = "insert into users(username,password) values(?,?)";
try {
Class.forName(driver);
// new oracle.jdbc.driver.OracleDriver();
Connection conn = DriverManager.getConnection(url, username,
password);
// Statement stat = conn.createStatement();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "samka");
pstmt.setString(2, "123");
pstmt.executeUpdate();
pstmt.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//查询oracle中的数据
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.1.224:1521:ora9";
String username = "scott";
String password = "root";
String sql = "select * from users";
try {
Class.forName(driver);
// new oracle.jdbc.driver.OracleDriver();
Connection conn = DriverManager.getConnection(url, username,
password);
// Statement stat = conn.createStatement();
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println("name:" + rs.getString("username")
+ "/tpassword:" + rs.getString("password"));
}
rs.close();
pstmt.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//对MySql进行插入操作
import java.sql.*;
public class register
{
/**
* @param args
*/
String name="samka";
String gender="male";
String phone="8873293";
String introduction="I am a student!";
public void setsName(String name)
{
this.name = name;
}
public String getsName()
{
return name;
}
public void setsGender(String gender)
{
this.gender = gender;
}
public String getsGender()
{
return gender;
}
public void setsPhone(String phone)
{
this.phone = phone;
}
public String getsPhone()
{
return phone;
}
public void setsIntroduction(String introduction)
{
this.introduction = introduction;
}
public String getsIntroduction()
{
return introduction;
}
public void insert()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/service","root","root");
//不明白下面两个参数?
Statement stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//插入语句:executeUpdate
stat.executeUpdate("insert into register(name,gender,phone,introduction)values('"+this.getsName()+"','"+this.getsGender()+"','"+this.getsPhone()+"','"+this.getsIntroduction()+"')");
}
catch(SQLException e)
{
System.out.print("插入失败"+e);
}
catch(Exception e)
{
System.out.print("有异常"+e);
}
}
public static void main(String[] args)
{
register reg = new register();
reg.insert();
}
}
//对MySql的查询操作
import java.sql.*;
public class select
{
public static void main(String[] args)
{
String rowdata="rfghtryhthy";
ResultSet result;
try{
Connection con;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/service","root","root");
Statement stat = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
result = stat.executeQuery("select * from register");
//获得结果集元数据
ResultSetMetaData rsmd = result.getMetaData();
//得到结果集中的列数
int colCount = rsmd.getColumnCount();
//得到结果集中第一列的列名
rowdata = rsmd.getColumnName(1).trim();
//依次得到其余列的列名
for(int i=2; i<=colCount; i++)
{
rowdata = rowdata+" : "+rsmd.getColumnName(i).trim();
}
System.out.println(rowdata);
System.out.println("----------------------------------------------------");
while(result.next())
{
rowdata = result.getString(1).trim();
for(int i=2; i<=colCount; i++)
{
rowdata = rowdata+" : "+result.getString(i);
}
System.out.println(rowdata);
System.out.println("/n");
}
}
catch(Exception e)
{
System.out.print("有异常"+e);
}
}
}