① java连接数据库的代码
package mysql;
import java.sql.*;
/**
* @author xys
*/
public class ConnectMysql {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/databaseName";
String user = "mysqluser";
String password = "password";
String driverClass = "com.mysql.cj.jdbc.Driver";
Connection connection = null;
Class.forName(driverClass);
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
if (connection != null) {
System.out.println("数据库连接成功");
} else {
System.out.println("数据库连接失败");
connection.close();
}
return connection;
}
public void getResult() throws ClassNotFoundException, SQLException {
// 实例化 Statement 对象
Statement statement = getConnection().createStatement();
// 要执行的 Mysql 数据库操作语句(增、删、改、查)
String sql = "";
// 展开结果集数据库
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
// 通过字段检索
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
// 输出数据
System.out.println("ID : " +id);
System.out.println("name :" + name);
}
// 完成后需要依次关闭
resultSet.close();
statement.close();
getConnection().close();
}
}
② java怎样通过查询语句获得数据库里的数据
Statement stm=conn.createStatement();
ResultSet rs=stm.execute("查询语句");
他会返回一个ResultSet 结果集
然后通过rs.next()方法便利结果集中的值
代码示例:
Class.forName("驱动地址");
Connection con=DriverManager.getConnection("数据库地址","用户名","密码");
Statement stm=con.createStatement();
ResultSet rs=stm.execute("查询语句");
while(rs.next()){
String str=rs.getString("对应的列名");
String str1=rs.getString(2);
int i=rs.getInt(3);
}
③ 用Java怎样访问数据库,用什么代码
1. 加载一个对应数据库的JDBC驱动
在建立到一个数据库的连接之前,必须先加载这个数据库的JDBC驱动程序,加载之后此driver会自动注册到JDBC驱动列表中。加载一个JDBC驱动有两种方法。
a) 在命令行方式下指定驱动器或者用冒号分割驱动器列表:
具体命令如下:
C:\>java –Djdbc.drivers = com.company1.Driver:com.company2.Driver youProject
b)第二种方法,在程序中调用Class.forName()方法。推荐使用。。。。
try
{
String driverName = “com.imaginary.sql.msql.MsqlDriver”;
Class.forName(driverName).newInstance();
}
Catch(ClassNotFoundException e1)
{
//catch could not find database driver exception.
}
2.连接到数据库。
根据您后台待连接的数据库不同,而有小小的差别。
a) 连接到Oracle数据库。
Connection connection = null ;
try
{
//load the jdbc driver ;
String driverName = “oracle.jdbc.driver.OracleDriver”;
Class.forName(driverName).newInstance();
//create a connection to the database;
String serverName = “127.0.0.1”;
String serverPort = “1521”;
String serverID = “datebase1”
String userName = “hello”;
String userPsw = “world”;
String url = “jdbc:oracle.thin:@” + serverName + “:” + serverPort + “:” + serverID ;
Connection = DriverManager.getConnection(url , userName , userPsw);
}
catch(ClassNotFoundException e1)
{
//catch could not find database driver exception.
}
catch(SQLException e2)
{
//catch could not connect to the database exception.
}
b) 连接到一个SQL Server数据库。
Connection connection = null ;
try
{
//load the jdbc driver ;
String driverName = “com.microsoft.jdbc.sqlserver.SQLServerDriver”;
Class.forName(driverName).newInstance();
//create a connection to the database;
String serverName = “127.0.0.1”;
String serverPort = “1433”;
String serverID = serverName + serverPort ;
String userName = “hello”;
String userPsw = “world”;
String url = “jdbc:JSQLConnect ://” + serverID ;
Connection = DriverManager.getConnection(url , userName , userPsw);
}
catch(ClassNotFoundException e1)
{
//catch could not find database driver exception.
}
catch(SQLException e2)
{
//catch could not connect to the database exception.
}
c) 连接到一个MySQL数据库上。。。。
Connection connection = null ;
try
{
//load the jdbc driver ;
String driverName = “org.gjt.mm.mysql.Driver”;
Class.forName(driverName).newInstance();
//create a connection to the database;
String serverName = “127.0.0.1”;
String serverID = “database”;
String userName = “hello”;
String userPsw = “world”;
String url = “jdbc:mysql ://” + serverName + “/” + serverID ;
Connection = DriverManager.getConnection(url , userName , userPsw);
}
catch(ClassNotFoundException e1)
{
//catch could not find database driver exception.
}
catch(SQLException e2)
{
//catch could not connect to the database exception.
}
综合上面的三种数据库连接方式 , 其实大同小异。由于访问不同的数据库和所使用的数据库驱动程序不同,所以导致代码表面上有小小不同,但透过表面看来,内部都是
1. 加载一个特定的数据库JDBC驱动。
2. 连接到一个数据库。
3. 之后,就可以对一个特定的数据库进行特定的操作了。
附上各种数据库的JDBC驱动起可用信息网址:
http://java.sun.com/procts/jdbc
对于Oracle数据库,请参考:
http://otn.oracle.com.software/content.html
对于MySQL数据库,请参考:
http://mmMySQL.sourceforge.net
对于SQL Server数据库,有很多的驱动可选,比较常用的:
http://www.microsoft.com/china/sql/downloads/2000/jdbc.asp
http://www.freetds.org
http://www.datadirect-technologies.com
④ 如何用Java实现数据库查询
import java.sql.*;
public class MSSQLText
{
public static void main(String args[])
{
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=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);
}
}
}