数据库连接项目
第一种:package 数据库连接;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class text1{
//数据库加载程序
private static final String DBDRIVER="oracle.jdbc.driver.OracleDriver";
//数据库的地址
private static final String DBURL="jdbc:oracle:thin:@localhost:1521:orcl";
//数据库的用户名和密码
private static final String DBUSR="scott";
private static final String PASS="cyq123";
public static void main(String[] args) {
//数据库的链接
Connection conn=null;
try {
//数据库的加载
Class.forName(DBDRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//用户数据的链接
try {
conn=DriverManager.getConnection(DBURL, DBUSR, PASS);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("链接成功");
try {
//关闭数据库
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第二种:
package 数据库连接;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
public class text2 {
private static final String DBDRIVEL="oracle.jdbc.drivel.OracleDrivel";
private static final String DBURL="jdbc:oracle:thin:@localhost:1521:orcl";
private static final String DBUSER="scott";
private static final String DBPASS="cyq123";
public static void main(String[] args) throws Exception {
Connection conn=null;
Statement stamt=null;
Class.forName(DBDRIVEL);
String sql="insert into USERS(name,password,age,sex,birthday)valuer('小明','admin',21,'男',sysdate)";
conn=DriverManager.getConnection(DBURL, DBUSER, DBPASS);
stamt=conn.createStatement();
stamt.executeUpdate(sql);
stamt.close();
conn.close();
}
}
第三种:
package 数据库连接;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Connection;
public class text3 {
private static final String DBDRIVER="oracle.jdbc.driver.OracleDriver";
private static final String DBURL="jdbc:oracle:thin:@localhost:1521:orcl";
private static final String DBUSER="scott";
private static final String DBPASS="cyq123";
public static void main(String[] args) throws Exception{
Connection conn=null;
Statement stmt=null;
Class.forName(DBDRIVER);
int id=1;
String name="小麦";
String sex="男";
String password="cyq123";
String birthday="27-8-16";
int age=26;
String sql="update USERS set name= "+name+"";
conn=DriverManager.getConnection(DBURL, DBUSER, DBPASS);
conn.close();
}
}
第四:
package 数据库连接.lib;
import java.sql.Connection;
import java.sql.DriverManager;
public class text {
private static final String DBDRIVER="oracle.jdbc.driver.OracleDriver";
private static final String DBURL="jdbc:oracle:thin:@localhost:1521:orcl";
private static final String DBUSER="system";
private static final String DBPASS="cyq123";
public static void main(String[] args) throws Exception{
//链接数据库
Connection conn=null;
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL, DBUSER, DBPASS);
conn.close();
}
}